package com.example.testapplication.viewmodel;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.testapplication.R;

public class CustomViewModelActivity extends AppCompatActivity {
    // 第二步 声明ViewModel
    private CustomViewModel viewModel;
    private TextView mCxkTv,mJayTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_view_model);
        mCxkTv = findViewById(R.id.tv_cxk);
        mJayTv = findViewById(R.id.tv_jay);
        // 第三步 实例化ViewModel
        viewModel = new ViewModelProvider(this).get(CustomViewModel.class);
        // 赋值 初始值
        mCxkTv.setText(String.valueOf(viewModel.getTicketCxk()));
        // 赋值 初始值
        mJayTv.setText(String.valueOf(viewModel.getTicketJay()));

        findViewById(R.id.btn_cxk).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewModel.setTicketCxk(viewModel.getTicketCxk() +1);
                mCxkTv.setText(String.valueOf(viewModel.getTicketCxk()));
            }
        });
        findViewById(R.id.btn_jay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.setTicketJay(viewModel.getTicketJay() + 1);
                mJayTv.setText(String.valueOf(viewModel.getTicketJay()));
            }
        });

    }
}